package www.com.zhongge.stack;

import java.util.Arrays;

/**
 * @ClassName MyStack
 * @Description TODO
 * @Author 李明忠
 * @Date 2025/7/24 9:50
 * @Version 1.0
 */
public class MyStack {
    public int[] elem;
    public int usedSize;

    public MyStack() {
        this.elem = new int[2];
    }

    public void push(int val) {
        if(isFull()) {
            this.elem = Arrays.copyOf(elem,2*elem.length);
        }
        elem[usedSize] = val;
        usedSize++;
    }

    public boolean isFull() {
        return usedSize == elem.length;
    }

    /**
     * 出栈：得到栈顶元素后得将指针下移
     * @return 元素值
     */
    public int pop() {
        if (isEmpty()) {
            return -1;
        }
        return elem[--usedSize];
    }

    /**
     * 判空
     * @return
     */
    private boolean isEmpty() {
        return usedSize == 0;
    }

    /**
     * 获取栈顶元素：指针无需下移
     * @return
     */
    public int peek() {
        if (isEmpty()) {
            return -1;
        }
        return elem[usedSize - 1];
    }
}
